/*
 * File Name:               UIWindow.cs
 *
 * Description:             UI窗口逻辑基类
 * Author:                  lisiyu <576603306@qq.com>
 * Create Date:             2016/11/22
 */

using System;
using UnityEngine;

public abstract class UIWindow : UIView
{
    // 窗口名称
    public string WinName;

    // 语言包
    private UILanguage mLanguage;

    // 额外参数
    private UIParam mParam;

    // 动画效果
    protected UIAnimator mAnimator;

    // 根节点
    private Transform mTransform;

    // 显示层级
    public UIManager.ELayer pLayer;

    // 占用的canvas层数
    public UIWidth mWidth;

    // 内部通信用事件
    // private XEventDispatcher<int> mEventDispatcher;
    // todo 考虑是否加入共享数据池来让各个View在Window内部交流数据

    public override void onSpawn(object args)
    {
        // 涉及到可变参数 初始化交给 onCreated
    }

    public void onCreated(string winName, GameObject obj, object args)
    {
        WinName = winName;

        var context = obj.GetComponent<UIContext>();
        mLanguage = context.getComponent<UILanguage>();
        mParam = context.getComponent<UIParam>();
        mAnimator = context.getComponent<UIAnimator>();
        pLayer = context.getComponent<UILayer>().pLayer;
        mWidth = context.getComponent<UIWidth>();

        mTransform = obj.transform;
        // mEventDispatcher = XCachePool.Singleton.pop<XEventDispatcher<int>>();

        base.onCreated(context, this, args);
    }

    public override void onRecycle()
    {
        if (mAnimator == null)
        {
            onResRecycle();
        }
        else
        {
            mAnimator.onClose(onResRecycle);
        }
    }

    private void onResRecycle()
    {
        base.onRecycle();

        // mEventDispatcher = null;
        mAnimator = null;
        mParam = null;
        mLanguage = null;
        WinName = null;
    }

    /// <summary>
    /// 是否需要回收游戏对象 
    /// </summary>
    protected override bool mNeedRecycleGameobject
    {
        get
        {
            return true;
        }
    }

    /// <summary>
    /// 获取语言包 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    protected override string getLanguage(int id)
    {
        if (mLanguage == null)
            return null;

        if (mLanguage.pLanguages == null)
            return null;

        if (mLanguage.pLanguages.Length < id)
            return null;
        // todo 先用描述替代着
        return mLanguage.pLanguages[id].pDescription;
    }

    /// <summary>
    /// 获取额外参数 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    protected override string getParam(int id)
    {
        if (mParam == null)
            return null;

        if (mParam.pParams == null)
            return null;

        if (mParam.pParams.Length < id)
            return null;

        return mParam.pParams[id].pContent;
    }

    /// <summary>
    /// 关闭自身 
    /// </summary>
    protected void closeSelf()
    {
        UIManager.Singleton.close(WinName);
    }
    
    /// <summary>
    /// 设置父节点
    /// </summary>
    /// <param name="parent"></param>
    public void setParentTransform(Transform parent)
    {
        if (mTransform != null)
        mTransform.SetParent(parent, false);
    }


    public void showOrHide(bool show)
    {
        if (show)
        {
            mContext.showByScale();
        }
        else
        {
            mContext.hideByScale();
        }
    }

    #region 内部通信

    ///// <summary>
    ///// 绑定消息处理函数 
    ///// </summary>
    ///// <param name="msgId">   </param>
    ///// <param name="callback"></param>
    //protected override void bindMessage(int msgId, Action<object[]> callback)
    //{
    //    mEventDispatcher.addListener(msgId, callback);
    //}

    ///// <summary>
    ///// 解绑消息处理函数 
    ///// </summary>
    ///// <param name="msgId">   </param>
    ///// <param name="callback"></param>
    //protected override void unbindMessage(int msgId, Action<object[]> callback)
    //{
    //    mEventDispatcher.removeListener(msgId, callback);
    //}

    ///// <summary>
    ///// 发送消息 
    ///// </summary>
    ///// <param name="msgId"></param>
    ///// <param name="args"> </param>
    //protected override void sendMessage(int msgId, params object[] args)
    //{
    //    mEventDispatcher.dispatch(msgId, args);
    //}

    #endregion 内部通信

    /// <summary>
    /// 窗口打开时的处理 
    /// </summary>
    public virtual void onOpened()
    {
        // TODO: 临时处理，避免UI关闭动画造成遮挡
        mTransform.SetAsFirstSibling();
    }
}